﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class LapPhieuThuTienBLL : BaseBLL
    {
        DAL.PhieuThuDAO _PhieuThuDAO;
        BLL.QuiDinhBLL _QuiDinhBLL;
        DAL.KhachHangDAO _KhacHangDAO;

        public LapPhieuThuTienBLL()
        {
            _PhieuThuDAO = new DAL.PhieuThuDAO();
            _QuiDinhBLL = new BLL.QuiDinhBLL();
            _KhacHangDAO = new DAL.KhachHangDAO();
        }

        public int LapPhieu(int IDKhachHang, DateTime NgayThu, float SoTien)
        {
            DTO.PhieuThuDTO dto = new DTO.PhieuThuDTO();
            dto.IDKhachHang = IDKhachHang;
            dto.NgayThu = NgayThu;
            dto.SoTien = SoTien;

            DTO.KhachHangDTO khachhangDto = new DTO.KhachHangDTO();
            khachhangDto.ID = IDKhachHang;

            if (CheckQD_4(ref khachhangDto, SoTien) != 0)
                return 1;
            _KhacHangDAO.Update(null, null, khachhangDto);
            _PhieuThuDAO.Insert(null, null, dto);
            return 0;
        }

        private int CheckQD_4(ref DTO.KhachHangDTO dto, float sotien)
        {
            if (_KhacHangDAO.Select(null, null, ref dto) <= 0)
                return 1; // khach hang ko ton tai
            if (_QuiDinhBLL.QD4 == true)
            {
                if (sotien > dto.DuNo)
                    return 2; // qui dinh 4
            }
            dto.DuNo -= sotien;
            return 0;
        }
    }
}
